package com.proracing.app.eitinerer.activities;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.proracing.app.eitinerer.db.mc_DBHandler;
import com.proracing.app.eitinerer.helpers.mc_Constants;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @description Main activity of eItinerer Application.
 *              Detailed informations contain technical documentation
 * @author Mateusz Skadorwa
 */

public class mc_MainActivity extends Activity
{
    private String lv_Login, lv_Pass;
    private EditText lr_Pass, lr_Login;
    private Dialog lr_dialog;
    private mc_Constants lr_cons;
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        lr_cons = new mc_Constants();
        
        lr_Login = (EditText)findViewById(R.id.login_field);
        lr_Pass = (EditText)findViewById(R.id.pass_field);
        
        lr_dialog = new Dialog(mc_MainActivity.this);
        Button lr_ButtonStart = (Button)findViewById(R.id.buttonLogin);
        lr_ButtonStart.getBackground().setAlpha(90);                            //making the field transparent
        lr_ButtonStart.setOnClickListener(loginListener); 
    }
    
    private final OnClickListener loginListener = new OnClickListener() {
        public void onClick(View v) {
            lv_Login = lr_Login.getText().toString();
            lv_Pass = lr_Pass.getText().toString();
            mc_DBHandler lr_dbhandler = new mc_DBHandler();
            String lv_result = "";
            try {
                lv_result = lr_dbhandler.connect(lv_Login, lv_Pass);            //check the login in DB
            } catch (URISyntaxException ex) {
                Logger.getLogger(mc_MainActivity.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(mc_MainActivity.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            if ( lv_result.equals(lr_cons.mc_organizer) ) {
                Intent lr_Intent = new Intent(mc_MainActivity.this, mc_MainOrganizerActivity.class);
                startActivity(lr_Intent);
            }
            else if ( lv_result.equals(lr_cons.mc_member) ) {
                Intent lr_Intent = new Intent(mc_MainActivity.this, mc_MainMemberActivity.class);
                startActivity(lr_Intent);
            }
            else if ( lv_result.equals("") ) {
                Intent lr_Intent = new Intent(mc_MainActivity.this, mc_LoginActivity.class);
                lr_Intent.putExtra("Login", lv_result);
                startActivity(lr_Intent);
            }
            else {            
                lr_dialog.setContentView(R.layout.dialog);
                lr_dialog.setTitle("Ostrzeżenie");
 
                // set values for custom dialog components - text, image and button
                TextView text = (TextView) lr_dialog.findViewById(R.id.textDialog);
                text.setText("Błędny login lub hasło.");
                ImageView image = (ImageView) lr_dialog.findViewById(R.id.imageDialog);
                image.setImageResource(R.drawable.error);
 
                lr_dialog.show();
                 
                lr_Login.setText("", TextView.BufferType.NORMAL);
                lr_Pass.setText("", TextView.BufferType.NORMAL);
            }                      
        }
    };
     
}
